<?php
require_once IA_ROOT.'/addons/goouc_class/inc/common/wxpay/log.php';
require_once IA_ROOT.'/addons/goouc_class/inc/common/wxpay/WxPayConfig.php';
require_once IA_ROOT.'/addons/goouc_class/inc/common/wxpay/lib/WxPayData.php';
require_once IA_ROOT.'/addons/goouc_class/inc/common/wxpay/lib/WxPayApi.php';
global $_W,$_GPC;
$contion = " p.weid = :weid";
$params[':weid'] = $_W['uniacid'];
$op = empty($_GPC['op'])?'payment':$_GPC['op'];
if($op == 'payment'){
    $type = intval($_GPC['type']);
    if($type == 1){
        $contion .= " AND p.type = :type";
        $params[':type'] = $type;
    }elseif ($type == 2) {
        $contion .= " AND (p.type = 2 OR p.type = 3)";
    }

    $order_status = intval($_GPC['order_status']);
    if($order_status == 1 || $order_status == 2){
        $contion .= " AND p.order_status = :order_status";
        if($order_status == 1){
            $params[':order_status'] = 1;
        }elseif($order_status == 2){
            $params[':order_status'] = 0;
        }
    }
    if(!empty($_GPC['time'])){
        $starttime = strtotime($_GPC['time']['start']);
        $endtime = strtotime($_GPC['time']['end']) +86399;
        $contion .= " AND p.createtime >= :starttime AND p.createtime <= :endtime";
        $params[':starttime'] = $starttime;
        $params[':endtime'] = $endtime;
    }else{
        $starttime = strtotime('-1 month');
        $endtime = time();
    }

    $pindex = max(1,$_GPC['page']);
    $psize = 15;
    $list = pdo_fetchall("SELECT p.type,p.userid,p.order_status,p.all_money,p.true_money,p.createtime,p.dataid,p.paytime,u.id as userid,u.nickname,u.headimg FROM ".tablename('goouc_class_order')." p JOIN ".tablename('goouc_class_user')." u ON p.userid = u.id WHERE ".$contion." ORDER BY createtime DESC LIMIT ".($pindex - 1)*$psize.",".$psize,$params);
    $total = pdo_fetchcolumn("SELECT COUNT(*) FROM ".tablename('goouc_class_order')." p JOIN ".tablename('goouc_class_user')." u ON p.userid = u.id WHERE ".$contion,$params);
    $page = pagination($total,$pindex,$psize);
    $money = 0;
    foreach($list as $k=>$v){
        if($v['order_status'] == 1){
            $money += $v['true_money'];
        }
        $list[$k]['title'] = pdo_fetchcolumn("select title from ".tablename($this->t_lesson)." where id = :id ",array(':id'=>$v['dataid']));
    }
}elseif ($op == 'group') {
    $pindex = max(1,$_GPC['page']);
    $psize = 15;
    //获得所有 过期未完成的团
    $list = pdo_fetchall("select ug.id,ug.createtime,ug.status,ug.groupnum,ug.lessonid,ug.status,u.nickname,u.headimg,u.myheaderimg from ".tablename($this->t_user_group)." ug JOIN ".tablename($this->t_user)." u ON ug.uid = u.id where ug.weid = :weid  order by ug.createtime DESC limit ".($pindex - 1)*$psize.",".$psize,array(':weid'=>$_W['uniacid']));
    $total = pdo_fetchcolumn("select COUNT(*) from ".tablename($this->t_user_group)." ug JOIN ".tablename($this->t_user)." u ON ug.uid = u.id where ug.weid = :weid ",array(':weid'=>$_W['uniacid']));
    foreach($list as $k=>$v){
        if(!empty($v['myheaderimg'])){
            $list[$k]['headerimg'] = $v['myheaderimg'];
        }else{
            $list[$k]['headerimg'] = $v['headimg'];
        }
        unset($list[$k]['myheaderimg']);
        unset($list[$k]['headimg']);
        $list[$k]['title'] = pdo_fetchcolumn("select title from ".tablename($this->t_lesson)." where id = :id ",array(':id'=>$v['lessonid']));
    }
    $page = pagination($total,$pindex,$psize);
}elseif($op == 'refund') {
    if ($_W['isajax']) {
        $groupid = $_GPC['groupid'];
        $userlist = pdo_fetchall("select o.out_trade_no,o.transaction_sn,o.all_money,o.true_money,o.order_status,ugi.uid,o.id from " . tablename($this->t_user_group_info) . " ugi JOIN " . tablename($this->t_order) . " o ON ugi.out_trade_no = o.out_trade_no where ugi.groupid = :groupid and ugi.weid = :weid", array(':groupid' => $groupid, ':weid' => $_W['uniacid']));

        foreach ($userlist as $k => $v) {
            if ($v['order_status'] == 1) {
                $out_trade_no = $v['out_trade_no'];
                $total_fee = $v['true_money'] * 100;
                $refund_fee = $v['true_money'] * 100;
                $input = new WxPayRefund();
                $input->SetOut_trade_no($out_trade_no);
                $input->SetTotal_fee($total_fee);
                $input->SetRefund_fee($refund_fee);
                $config = new WxPayConfig();
                $input->SetOut_refund_no("refund" . date("YmdHis"));
                $input->SetOp_user_id($config->GetMerchantId());
                $result = WxPayApi::refund($config, $input);
                if ($result['result_code'] == 'SUCCESS' && $result['return_code'] == 'SUCCESS') {
                    pdo_update($this->t_order, array('order_status' => 2, 'endtime' => TIMESTAMP), array('id' => $v['id']));
                }
            }
        }
        $message = array('success'=>200,'msg'=>'退款成功');
        echo json_encode($message);exit;
    }
}

include $this->template('web/payment');
?>